const WXBizDataCrypt = require('../middleware/WXBizDataCrypt')
const {createwxUser,uploadtx} = require("../service/wx.user.server")
const jwt = require("jsonwebtoken");

class WxWxCrypt {
    async wxyanzhen(ctx, next) {
        const resultData = await ctx.request.body
        var appId = resultData.appid
        var sessionKey = resultData.sessionData
        var encryptedData = resultData.encryptedData
        var iv = resultData.iv
        var pc = new WXBizDataCrypt(appId, sessionKey)
        var data = await pc.decryptData(encryptedData, iv)
        const {openId} = data
        const my = "zddata"
        const res = await createwxUser(openId)

        const resd = jwt.sign({openId: openId}, my, {expiresIn: '10d'})
        console.log("jiami后",resd)
        var user=jwt.verify(resd,'zddata')
        console.log("resd加密", resd)
        console.log(user,"解密后的数据")
        ctx.body = {
            code:200,
            result:resd
        }
    }
    async uploadtx(ctx){
       const {tx}=ctx.request.body
        console.log(ctx.state.wxID)
        const res=await uploadtx(tx,ctx.state.wxID)
        ctx.body="ad"
    }

}

module.exports = new WxWxCrypt()